import request from "@/axios";
import store from "@/store";
import axios from "axios";
import { config } from "@/global";
const prefix = "/file";
const getFileByDir = function(dirId) {
  return request({
    url: prefix + "/getUserFileByDirId",
    method: "post",
    params: { dirId }
  });
};

const getFileFromRecycleBinByDir = function(dirId) {
  return request({
    url: prefix + "/getUserFileFromRecycleBinByDirId",
    method: "post",
    params: { dirId }
  });
};

const getAllFileType = function() {
  return request({
    url: prefix + "/getAllFileType"
  });
};

const getUsage = function() {
  return request({
    url: prefix + "/userUsage",
    method: "post"
  });
};

const createDir = function({ name, parentId }) {
  return request({
    url: prefix + "/createDir",
    method: "post",
    data: { name, parentId, userId: store.state.world.userInfo.id }
  });
};

const deleteFile = function(dirId) {
  return request({ url: prefix + "/delete/" + dirId });
};

const recoverFileFromRecycleBin = function(fileId) {
  return request({
    url: prefix + "/recoverFromRecycleBin/" + fileId
  });
};

const fileExists = function(fileMD5, parentId, filename) {
  return request({
    url: "/file/flashDetection",
    method: "post",
    params: { fileMd5: fileMD5, fileParentId: parentId, fileName: filename }
  });
};

const deleteFileFromRecycleBin = function(fileId) {
  return request({
    url: prefix + "/deleteFromRecycleBin/" + fileId
  });
};

let chunkHandlerCancelToken;

const chunkHandler = async function(md5, size) {
  return new Promise((resolve, reject) => {
    axios
      .post(config.baseURL + "/file/chunkHandler", null, {
        params: { md5, size },
        headers: { token: store.state.world.token },
        cancelToken: new axios.CancelToken(function executor(c) {
          chunkHandlerCancelToken = c;
        })
      })
      .then(res => resolve(res.data.data))
      .catch(err => reject(err));
  });
};

let uploadCancelToken;

const uploadHandler = function(
  chunk,
  md5,
  size,
  chunkIndex,
  chunkMD5,
  filename,
  parentId
) {
  let data = new FormData();
  data.append("uploadedChunk", chunk);
  data.append("fileMd5", md5);
  data.append("fileSize", size);
  data.append("chunkIndex", chunkIndex);
  data.append("chunkMd5", chunkMD5);
  data.append("fileName", filename);
  if (parentId != null) {
    data.append("parentId", parentId);
  }
  return axios.post(config.baseURL + "/file/uploadFile", data, {
    "Content-Type": "multipart/form-data",
    cancelToken: new axios.CancelToken(function executor(c) {
      uploadCancelToken = c;
    }),
    headers: { token: store.state.world.token }
  });
};

const renameFile = function(fileId, name) {
  return request({
    url: prefix + "/renameUserFile",
    method: "post",
    data: { id: fileId, name }
  });
};

const moveUserFile = function(fileId, dirId) {
  return request({
    url: prefix + "/moveUserFile",
    method: "post",
    data: { id: fileId, parentId: dirId }
  });
};

const searchFile = function(filename) {
  return request({
    url: prefix + "/search/" + filename,
    method: "post"
  });
};

const copy = function(fileId, to) {
  return request({
    url: prefix + "/copy/" + fileId + "/" + to
  });
};

const share = function(fileId, shareDays) {
  return request({
    url: "/s/getLink/" + fileId + "/" + shareDays
  });
};

export {
  getFileByDir,
  getAllFileType,
  getUsage,
  createDir,
  deleteFile,
  getFileFromRecycleBinByDir,
  recoverFileFromRecycleBin,
  deleteFileFromRecycleBin,
  fileExists,
  chunkHandler,
  chunkHandlerCancelToken,
  uploadHandler,
  uploadCancelToken,
  renameFile,
  moveUserFile,
  searchFile,
  copy,
  share
};
